<?php
/**
 * Zend_Acl object with user rules
 *
 * @author     Mario Schillermann <info@rockit-flash.com>
 * @encoding   UTF-8 äöüßÖÄÜ
 * @copyright  © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license    GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @uses       Zend_Db_Table_Abstract
 * @category   Module
 * @package    Login
 * @subpackage Model
 * @todo
 */

class Login_Model_Acl extends Zend_Db_Table_Abstract
{
    /**
     * Get Zend_Acl object with user rules
     * 
     * @param  string $roleId User role
     * @return Zend_Acl
     */
    public function getRules($roleId)
    {
        try {
            /** Get role name  */
            $query = $this->_db
                          ->select()
                          ->from("access_roles", array("rolename"))
                          ->where('roleId' . ' = ?', $roleId);
            $access = $this->_db->fetchOne($query);
            //$roleName = $access["rolename"];
die(var_dump($access));
            $acl = new Zend_Acl;
            /** Add role */
            $acl->addRole(new Zend_Acl_Role($roleName));

            if ($access["role"] == "admin") {
                $acl->allow($access['role']);
                return $acl;
            }
            /** Get rules */
            $query = $this->_db
                          ->select()
                          ->from('access_rules',
                              array('resource', 'privileg'))
                          ->where('roles_id' . ' = ?', $roleId);
            $resources = $this->_db->fetchAll($query);

            /** Set resource and privileg */
            foreach ($resources as $resource) {

                $acl->addResource($resource['resource']);
                $privileges = array();
                $privileges = explode(',', $resource['privileg']);
                $acl->allow(
                    $access['role'],
                    $resource['resource'],
                    $privileges
                );
            }
            return $acl;

        } catch (Zend_Exception $e) {
            error_log(print_r($e, true));
            return false;
        }
    }
}